<?

class Mdl_Comments extends MY_Model {

    function Mdl_Comments() {

        parent::MY_Model();

        $this->limit = 25;

    }

    function get($params = NULL) {

        $this->prep_params($params);

        if (!isset($params['select'])) {

            $this->db->select('
            inktype_comments.comment_id,
            inktype_comments.site_id,
            inktype_comments.post_id,
            inktype_comments.comment_date,
            inktype_comments.comment,
            inktype_comments.display_name,
            inktype_posts.title');

        }

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $offset = ($this->page * $this->limit) - $this->limit;

            $this->db->limit($this->limit, $offset);

        }

        if (isset($params['comment_id'])) {

            $this->db->where('comment_id', $params['comment_id']);

        }

        if (isset($params['post_id'])) {

            $this->db->where('inktype_comments.post_id', $params['post_id']);

        }

        if (!isset($params['order_by'])) {

            $this->db->order_by('inktype_comments.comment_date DESC');

        }

        if (!isset($params['show_spam'])) {

            $this->db->where('is_spam', 0);

        }

        if (isset($params['show_spam']) AND $params['show_spam'] == TRUE) {

            $this->db->where('is_spam', 1);

        }

        $this->db->join('inktype_posts', 'inktype_posts.post_id = inktype_comments.post_id');

        if (isset($this->is_admin)) {

            $this->db->where('inktype_comments.site_id', $this->session->userdata('site_id'));

        }

        else {

            $this->db->where('inktype_comments.site_id', $this->inktype->site_id);

        }

        $query = $this->db->get('inktype_comments');

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $this->set_page_links();

        }

        return $query->result();

    }

    function get_num_comments() {

        $this->db->select('COUNT(*) AS num_comments');

        $this->db->where('site_id', $this->session->userdata('site_id'));

        $query = $this->db->get('inktype_comments');

        $result = $query->row();

        return $result->num_comments;

    }

    function save() {

        $db_array = $this->prep_db_array();

        if (!isset($this->uri_assoc['comment_id'])) {

            $db_array['site_id'] = $this->session->userdata('site_id');

            $db_array['ip_address'] = $this->input->ip_address();

            $this->db->insert('inktype_comments', $db_array);

        }

        else {

            $this->db->where('comment_id', $this->uri_assoc['comment_id']);

            $this->db->update('inktype_comments', $db_array);

        }

        $this->session->set_flashdata('success_save', TRUE);

    }

    function user_comment() {

        if ($this->validate()) {

            $db_array = $this->prep_db_array();

            if ($this->session->userdata('user_id')) {

                $this->db->where('user_id', $this->session->userdata('user_id'));

                $query = $this->db->get('inktype_users');

                $result = $query->row();

                $db_array['display_name'] = $result->display_name;

            }

            $db_array['site_id'] = $this->inktype->site_id;

            $db_array['comment_date'] = time();

            $db_array['ip_address'] = $this->input->ip_address();

            if ($this->config->item('defensio_enabled')) {

                $this->load->library('defensio');

                if ($this->defensio->audit_comment($db_array['ip_address'], date('Y/m/d', $db_array['comment_date']), $db_array['display_name'], $db_array['comment'])) {

                    $db_array['is_spam'] = 1;

                }

            }

            $this->db->insert('inktype_comments', $db_array);

            redirect($this->uri->uri_string());

        }

    }

    function delete() {

        if (isset($this->uri_assoc['comment_id'])) {

            $this->db->where('comment_id', $this->uri_assoc['comment_id']);

            $this->db->delete('inktype_comments');

            $this->session->set_flashdata('success_delete', TRUE);

        }

    }

    function delete_multiple() {

        if ($this->input->post('spam_mass_select')) {

            $comment_ids = $this->input->post('spam_mass_select');

            foreach (array_keys($comment_ids) as $key) {

                $comment_ids[] = $key;

            }

            $this->db->where_in('comment_id', $comment_ids);

            $this->db->delete('inktype_comments');

        }

    }

    function not_spam() {

        if (isset($this->uri_assoc['comment_id'])) {

            $this->db->where('comment_id', $this->uri_assoc['comment_id']);

            $db_array = array(
                'is_spam'   =>  0
            );

            $this->db->update('inktype_comments', $db_array);

        }

    }

    function not_spam_multiple() {

        if ($this->input->post('spam_mass_select')) {

            $comment_ids = $this->input->post('spam_mass_select');

            foreach (array_keys($comment_ids) as $key) {

                $comment_ids[] = $key;

            }

            $this->db->where_in('comment_id', $comment_ids);

            $db_array = array(
                'is_spam'   =>  0
            );

            $this->db->update('inktype_comments', $db_array);

        }

    }

    function delete_all_spam() {

        $this->db->where('is_spam', 1);

        $this->db->delete('inktype_comments');

    }

    function validate() {

        $this->fields = array(
            'post_id'   =>  'Post',
            'comment'   =>  'Comment'
        );

        $this->rules = array(
            'post_id'   =>  'required',
            'comment'   =>  'required'
        );

        if (!$this->is_admin) {

            if ($this->inktype->is_logged_in()) {

                $this->fields['user_id'] = 'Comment Author';

                $this->rules['user_id']	= 'required';

            }

            else {

                $this->fields['display_name'] = 'Comment Author';

                $this->rules['display_name']	= 'required';

            }

        }

        return $this->_validate();

    }

}

?>